﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
using System.Web.UI.WebControls;
using System.Web.UI;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
using System.IO.Compression;
using Newtonsoft.Json;
using GPS.GPS_DAL;
using Microsoft.Office.Interop.Word;

namespace GPS.Controllers
{
    public class GPS_QuanLyBaoCaoIController : FDBController
    {
        #region unit
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public string contentFile { get; set; }
        public void initialCategorySearchAction(ViewModelQLDBCI viewModel)
        {
            string DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            ViewBag.checkuser = DVGTVT_MA;

            if (DVGTVT_MA != "40")
                viewModel.pBCI_DVGTVT_MA = DVGTVT_MA;

            //string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, viewModel.pBCI_DVGTVT_MA);
            //viewModel.pBCI_DVGTVT_MA = getdvgtvt_Ma;

            //string MaKhoaHoc = null;
            //GPS.DAL.Check_User.Substring_Autocomplate(viewModel.pBCI_KH_MA, ref MaKhoaHoc);
            //viewModel.pBCI_KH_MA = MaKhoaHoc;

            var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN1(viewModel.pBCI_DVGTVT_MA, viewModel.pBCI_KH_MA);
            ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => (o.DVGTVT_MAQL == DVGTVT_MA || o.DVGTVT_MA == DVGTVT_MA) && o.DVGTVT_ACTIVE == "1"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
            ViewBag.HANGDT_MAs = new SelectList(_db.GetAll<SHANGGPLX>(), "HGPLX_MA", "HGPLX_TEN");
            

        }

        public void initialCategorySearchAction_Log(ViewModelQLDBCI_Log viewModel)
        {
            string DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            ViewBag.checkuser = DVGTVT_MA;

            if (DVGTVT_MA != "40")
                viewModel.pBCI_DVGTVT_MA = DVGTVT_MA;

            //string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, viewModel.pBCI_DVGTVT_MA);
            //viewModel.pBCI_DVGTVT_MA = getdvgtvt_Ma;

            //string MaKhoaHoc = null;
            //GPS.DAL.Check_User.Substring_Autocomplate(viewModel.pBCI_KH_MA, ref MaKhoaHoc);
            //viewModel.pBCI_KH_MA = MaKhoaHoc;

            var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN1(viewModel.pBCI_DVGTVT_MA, viewModel.pBCI_KH_MA);
            ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => (o.DVGTVT_MAQL == DVGTVT_MA || o.DVGTVT_MA == DVGTVT_MA) && o.DVGTVT_ACTIVE == "1"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
            ViewBag.HANGDT_MAs = new SelectList(_db.GetAll<SHANGGPLX>(), "HGPLX_MA", "HGPLX_TEN");


        }

        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }
        #endregion

        #region Action

        public ActionResult Index(ViewModelQLDBCI SearchModel, int? currentPage)
        {
            try
            {
                initialCategorySearchAction(SearchModel);
                List<CrudModelDBCI> model = new List<CrudModelDBCI>();
                model = GetListData(SearchModel);


                if (SearchModel.SearchButton == "Xuất báo cáo 1")
                {
                    //khoi tao Word app:
                    Microsoft.Office.Interop.Word.Application _app = null;
                    Microsoft.Office.Interop.Word.Document _doc = null;
                    object _pathFile = null;
                    object ob = System.Reflection.Missing.Value;

                    try
                    {
                        // ExportDHV_BC1 _model = BindDataExport_BaoCao1(SearchModel);
                        DataSet ds = OracleHelper.ExecuteDataset(_db.DbContext.Database.Connection.ConnectionString, CommandType.StoredProcedure, "GPS_DAOTAO.GPS_DAOTAO_DSHVLX"
                                                        , new OracleParameter("PDVGTVT_MA", OracleDbType.Varchar2, string.IsNullOrWhiteSpace(SearchModel.pBCI_DVGTVT_MA) ? DBNull.Value : (object)SearchModel.pBCI_DVGTVT_MA, ParameterDirection.Input)
                                                        , new OracleParameter("PKH_MA", OracleDbType.Varchar2, string.IsNullOrWhiteSpace(SearchModel.pBCI_KH_MA) ? DBNull.Value : (object)SearchModel.pBCI_KH_MA, ParameterDirection.Input)
                                                        , new OracleParameter("PRETHEADER", OracleDbType.RefCursor, ParameterDirection.Output)
                                                        , new OracleParameter("PRETDSHVLX", OracleDbType.RefCursor, ParameterDirection.Output));
                        //GiangTD3 - 2017-08-29: 
                        string filename = "BC1_" + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".docx";
                        String GENERATED_FILE_NAME = System.Web.Hosting.HostingEnvironment.MapPath(@"~/Templates/GPS_WordExport/" + filename);
                        String TEMPLATE_FILE_NAME = System.Web.Hosting.HostingEnvironment.MapPath(@"~/Templates/GPS_Word/Template_BC1.dotx");
                        if (ds == null || ds.Tables.Count <= 0)
                        {
                            ViewBag.Message = "Không tìm thấy dữ liệu để xuất danh sách báo cáo 1.";
                            return RedirectToAction("Index");
                        }
                        //Template_BC1
                        //byte[] contentFile = null;
                        //contentFile = GPS.ENTITYS.EXPORTWORD_DHV_BC1.EXPORT_HOCVIENBC1(Server.MapPath("~/Templates/GPS_Word/BC1.docx"), _model, "DocxGenericReport");
                        //string nameOutput = string.Format("DanhSachBaoCao1_{0}_{0}.docx", _model.KH_TEN, DateTime.Now.ToShortDateString().Replace("/", "_"));
                        //return File(contentFile, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", nameOutput);

                        _pathFile = TEMPLATE_FILE_NAME;
                        _app = new Microsoft.Office.Interop.Word.Application();
                        _app.Visible = false;

                        _doc = _app.Documents.Add(ref _pathFile, ref ob, ref ob, ref ob);

                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            System.Data.DataTable vDataTable = ds.Tables[0];

                            Dictionary<string, string> dicKHOAHOC = new Dictionary<string, string>();
                            dicKHOAHOC.Add("DVGTVT_TEN", vDataTable.Rows[0]["DVGTVT_TEN"].ToString().ToUpper());

                            dicKHOAHOC.Add("KH_TEN", vDataTable.Rows[0]["KH_TEN"].ToString());
                            dicKHOAHOC.Add("KH_HANGGPLX_MA", vDataTable.Rows[0]["KH_HANGGPLX_MA"].ToString());
                            dicKHOAHOC.Add("KH_THOIGIANDT", vDataTable.Rows[0]["KH_THOIGIANDT"].ToString());
                            // dicKHOAHOC.Add("KH_TONGHV", vDataTable.Rows[0]["KH_TONGHV"].ToString());
                            //KH_NGAYKG1
                            var _NGAY_KG = Convert.ToDateTime(vDataTable.Rows[0]["KH_NGAYKG1"]);

                            dicKHOAHOC.Add("dd", (_NGAY_KG.Day < 10 ? "0" + _NGAY_KG.Day.ToString() : _NGAY_KG.Day.ToString()));
                            dicKHOAHOC.Add("mm", (_NGAY_KG.Month < 10 ? "0" + _NGAY_KG.Month.ToString() : _NGAY_KG.Month.ToString()));
                            dicKHOAHOC.Add("yyyy", _NGAY_KG.Year.ToString());

                            var _NGAY_BG = Convert.ToDateTime(vDataTable.Rows[0]["KH_NGAYBG1"]);
                            //   var _now = DateTime.Now;
                            dicKHOAHOC.Add("dd2", (_NGAY_BG.Day < 10 ? "0" + _NGAY_BG.Day.ToString() : _NGAY_BG.Day.ToString()));
                            dicKHOAHOC.Add("mm2", (_NGAY_BG.Month < 10 ? "0" + _NGAY_BG.Month.ToString() : _NGAY_BG.Month.ToString()));
                            dicKHOAHOC.Add("yyyy2", _NGAY_BG.Year.ToString());


                            foreach (Microsoft.Office.Interop.Word.Field field in _doc.Fields)
                            {
                                string fieldName = field.Code.Text.Substring(11, field.Code.Text.IndexOf("\\") - 12).Trim();
                                if (dicKHOAHOC.ContainsKey(fieldName))
                                {
                                    field.Select();
                                    _app.Selection.TypeText(dicKHOAHOC[fieldName]);
                                }
                            }

                            
                        }
                        //Thông tin  học viên
                        if (ds.Tables[1].Rows.Count > 0)
                        {

                            System.Data.DataTable vDataTable = ds.Tables[1];
                            Microsoft.Office.Interop.Word.Table tbl = _doc.Tables[2];


                            int lenrow = vDataTable.Rows.Count;
                            int lencol = vDataTable.Columns.Count;

                            for (int i = 0; i < lenrow; ++i)
                            {
                               
                              

                                for (int j = 0; j < lencol; ++j)
                                {


                                    tbl.Cell(i + 3, j + 1).Range.Text = vDataTable.Rows[i][j].ToString();// +"\v 2";
                                    //tbl.Cell(i + 2, j + 1).VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                                    tbl.Cell(i + 3, j + 1).Range.Font.Size = 10F;

                                }
                                if (i < lenrow - 1)
                                    tbl.Rows.Add(ref ob);
                            }
                        }
                       // _doc.Tables[2].Rows[ds.Tables[1].Rows.Count + 2].Delete();

                        try
                        {

                            object filenamesave = GENERATED_FILE_NAME;
                            _doc.SaveAs2(ref filenamesave);
                            _doc.Close(ref ob, ref ob, ref ob);
                            _doc = null;
                            _app.Quit(ref ob, ref ob, ref ob);
                            _app = null;
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                        finally
                        {
                            if (_doc != null)
                            {
                                _doc.Close(ref ob, ref ob, ref ob);
                                _doc = null;
                            }
                            if (_app != null)
                            {
                                _app.Quit(ref ob, ref ob, ref ob);
                                _app = null;
                            }

                        }

                        return File(GENERATED_FILE_NAME, "application/docx", filename);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        if (_doc != null)
                        {
                            _doc.Close(ref ob, ref ob, ref ob);
                            _doc = null;
                        }
                        if (_app != null)
                        {
                            _app.Quit(ref ob, ref ob, ref ob);
                            _app = null;
                        }
                    }

                  
                
                }
                else if (SearchModel.SearchButton == "Báo cáo chị Ngãi đòi không biết để làm gì")
                {
                    try
                    {
                        model = GetListData(SearchModel);

                        FileInfo template = new FileInfo(Server.MapPath(@"\Templates\GPS_Excel\DanhSachKhoahocBCI_TuNgayDenNgay.xlsx"));
                        FileInfo newFile = new FileInfo(Server.MapPath(@"\Templates\GPS_Excel\DanhSachKhoahocBCI_TuNgayDenNgay_Export.xlsx"));

                        using (OfficeOpenXml.ExcelPackage excelPackage = new OfficeOpenXml.ExcelPackage(newFile, template))
                        {
                            // Getting the complete workbook...
                            ExcelWorkbook myWorkbook = excelPackage.Workbook;
                            // Getting the worksheet by its name...
                            ExcelWorksheet myWorksheet = myWorkbook.Worksheets["Sheet1"];



                            int recordIndex = 4;
                            for (int i = 0; i < model.Count; i++)
                            {
                                //myWorksheet.InsertRow(recordIndex + i, 1, 4);

                                //myWorksheet.Cells[recordIndex + i, 1].Value = model[i].STT;
                                //myWorksheet.Cells[recordIndex + i, 1].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;



                                //myWorksheet.Cells[recordIndex + i, 2].Value = model[i].KH_TEN;
                                //myWorksheet.Cells[recordIndex + i, 2].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Left;

                                //myWorksheet.Cells[recordIndex + i, 3].Value = model[i].DVGTVT_TEN;
                                //myWorksheet.Cells[recordIndex + i, 3].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Left;

                                //myWorksheet.Cells[recordIndex + i, 4].Value = model[i].KH_HANGGPLX_MA;
                                //myWorksheet.Cells[recordIndex + i, 4].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;

                                //myWorksheet.Cells[recordIndex + i, 5].Value = model[i].KH_HANGDT_MA;
                                //myWorksheet.Cells[recordIndex + i, 5].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;

                                //myWorksheet.Cells[recordIndex + i, 6].Value = model[i].KH_SOQDKG;
                                //myWorksheet.Cells[recordIndex + i, 6].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Left;

                                ////myWorksheet.Cells[recordIndex + i, 7].Value = model[i].KH_NGAYQDKG_TEXT;
                                ////myWorksheet.Cells[recordIndex + i, 7].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Left;

                                //myWorksheet.Cells[recordIndex + i, 7].Value = model[i].KH_NGAYKG_TEXT;
                                //myWorksheet.Cells[recordIndex + i, 7].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Left;

                                //myWorksheet.Cells[recordIndex + i, 8].Value = model[i].KH_NGAYBG_TEXT;
                                //myWorksheet.Cells[recordIndex + i, 8].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Left;

                                //myWorksheet.Cells[recordIndex + i, 9].Value = model[i].KH_NGAYTHI_TEXT;
                                //myWorksheet.Cells[recordIndex + i, 9].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Left;

                                //myWorksheet.Cells[recordIndex + i, 10].Value = model[i].KH_NGAYSH_TEXT;
                                //myWorksheet.Cells[recordIndex + i, 10].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Left;

                                //myWorksheet.Cells[recordIndex + i, 11].Value = model[i].KH_TONGHV;
                                //myWorksheet.Cells[recordIndex + i, 11].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;

                                //myWorksheet.Cells[recordIndex + i, 12].Value = model[i].KH_MOTA;
                                //myWorksheet.Cells[recordIndex + i, 12].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Left;

                                string modelRange = "A" + (recordIndex + i).ToString() + ":L" + (recordIndex + i).ToString();
                                myWorksheet.Cells[modelRange].Style.Border.Top.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
                                myWorksheet.Cells[modelRange].Style.Border.Left.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
                                myWorksheet.Cells[modelRange].Style.Border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
                                myWorksheet.Cells[modelRange].Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
                                //  recordIndex++;

                            }

                            myWorksheet.DeleteRow(model.Count + recordIndex);

                            excelPackage.Save();
                        }
                        string contentType = string.Empty;
                        string CurrentFileName = Server.MapPath(@"\Templates\GPS_Excel\DanhSachKhoaHoc_Export.xlsx");
                        if (CurrentFileName.Contains(".xlsx"))
                        {
                            contentType = "application/msexcel";
                        }
                        return File(CurrentFileName, contentType, string.Format("DanhSachKhoaHoc_{0}", DateTime.Now.ToShortDateString().Replace("/", "_") + ".xlsx"));
                    }
                    catch (Exception ex)
                    {
                        ViewBag.Message = string.Format("Hello {0}.\\nCurrent Date and Time: {1}", "TEST", DateTime.Now.ToString());
                        return RedirectToAction("Index");
                    }
                }

                ViewBag.TotalRow = model == null ? "0" : model.Count().ToString();
                ViewBag.TotalHV = 0;
                ViewBag.TotalHV_TT = 0;
                if(model!=null)
                {
                    decimal _totalHV = 0;
                    decimal _totalHV_TT = 0;
                    foreach (var item in model)
                    {
                        _totalHV += item.BCI_SOHV == null ? 0 : item.BCI_SOHV.Value;
                        _totalHV_TT += item.BCI_SOHV_TT == null ? 0 : item.BCI_SOHV_TT.Value;
                    }
                        
                    ViewBag.TotalHV = _totalHV;
                    ViewBag.TotalHV_TT = _totalHV_TT;
                }

                var pageIndex = SearchModel.Page ?? 1;
                SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
                return View(SearchModel);
            }
            catch (Exception ex)
            {
                ViewBag.Message = string.Format("Lỗi hệ thống vui lòng liên hệ với admin.Lỗi: {0}", ex.ToString());
                return View(SearchModel);
            }
        }

        public ActionResult Search(ViewModelQLDBCI SearchModel, int? currentPage)
        {
            try
            {
                initialCategorySearchAction(SearchModel);
                List<CrudModelDBCI> model = new List<CrudModelDBCI>();
                model = GetListData(SearchModel);


                if (SearchModel.SearchButton == "Xuất báo cáo 1")
                {
                    //ExportDHV_BC1 _model = BindDataExport_BaoCao1(SearchModel);
                    //if (_model == null)
                    //{
                    //    ViewBag.Message = "Không tìm thấy dữ liệu để xuất danh sách báo cáo 1.";
                    //    return RedirectToAction("Index");
                    //}
                    //byte[] contentFile = null;
                    //contentFile = GPS.ENTITYS.EXPORTWORD_DHV_BC1.EXPORT_HOCVIENBC1(Server.MapPath("~/Templates/GPS_Word/BC1.docx"), _model, "DocxGenericReport");
                    //string nameOutput = string.Format("DanhSachBaoCao1_{0}_{0}.docx", _model.KH_TEN, DateTime.Now.ToShortDateString().Replace("/", "_"));
                    //return File(contentFile, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", nameOutput);

                    //khoi tao Word app:
                    Microsoft.Office.Interop.Word.Application _app = null;
                    Microsoft.Office.Interop.Word.Document _doc = null;
                    object _pathFile = null;
                    object ob = System.Reflection.Missing.Value;

                    try
                    {
                        // ExportDHV_BC1 _model = BindDataExport_BaoCao1(SearchModel);
                        DataSet ds = OracleHelper.ExecuteDataset(_db.DbContext.Database.Connection.ConnectionString, CommandType.StoredProcedure, "GPS_DAOTAO.GPS_DAOTAO_DSHVLX"
                                                        , new OracleParameter("PDVGTVT_MA", OracleDbType.Varchar2, string.IsNullOrWhiteSpace(SearchModel.pBCI_DVGTVT_MA) ? DBNull.Value : (object)SearchModel.pBCI_DVGTVT_MA, ParameterDirection.Input)
                                                        , new OracleParameter("PKH_MA", OracleDbType.Varchar2, string.IsNullOrWhiteSpace(SearchModel.pBCI_KH_MA) ? DBNull.Value : (object)SearchModel.pBCI_KH_MA, ParameterDirection.Input)
                                                        , new OracleParameter("PRETHEADER", OracleDbType.RefCursor, ParameterDirection.Output)
                                                        , new OracleParameter("PRETDSHVLX", OracleDbType.RefCursor, ParameterDirection.Output));
                        //GiangTD3 - 2017-08-29: 
                        string filename = "BC1_" + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".docx";
                        String GENERATED_FILE_NAME = System.Web.Hosting.HostingEnvironment.MapPath(@"~/Templates/GPS_WordExport/" + filename);
                        String TEMPLATE_FILE_NAME = System.Web.Hosting.HostingEnvironment.MapPath(@"~/Templates/GPS_Word/Template_BC1.dotx");
                        if (ds == null || ds.Tables.Count <= 0)
                        {
                            ViewBag.Message = "Không tìm thấy dữ liệu để xuất danh sách báo cáo 1.";
                            return RedirectToAction("Index");
                        }
                        //Template_BC1
                        //byte[] contentFile = null;
                        //contentFile = GPS.ENTITYS.EXPORTWORD_DHV_BC1.EXPORT_HOCVIENBC1(Server.MapPath("~/Templates/GPS_Word/BC1.docx"), _model, "DocxGenericReport");
                        //string nameOutput = string.Format("DanhSachBaoCao1_{0}_{0}.docx", _model.KH_TEN, DateTime.Now.ToShortDateString().Replace("/", "_"));
                        //return File(contentFile, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", nameOutput);

                        _pathFile = TEMPLATE_FILE_NAME;
                        _app = new Microsoft.Office.Interop.Word.Application();
                        _app.Visible = false;

                        _doc = _app.Documents.Add(ref _pathFile, ref ob, ref ob, ref ob);

                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            System.Data.DataTable vDataTable = ds.Tables[0];

                            Dictionary<string, string> dicKHOAHOC = new Dictionary<string, string>();
                            dicKHOAHOC.Add("DVGTVT_TEN", vDataTable.Rows[0]["DVGTVT_TEN"].ToString().ToUpper());

                            dicKHOAHOC.Add("KH_TEN", vDataTable.Rows[0]["KH_TEN"].ToString());
                            dicKHOAHOC.Add("KH_HANGGPLX_MA", vDataTable.Rows[0]["KH_HANGGPLX_MA"].ToString());
                            dicKHOAHOC.Add("KH_THOIGIANDT", vDataTable.Rows[0]["KH_THOIGIANDT"].ToString());
                            // dicKHOAHOC.Add("KH_TONGHV", vDataTable.Rows[0]["KH_TONGHV"].ToString());
                            //KH_NGAYKG1
                            var _NGAY_KG = Convert.ToDateTime(vDataTable.Rows[0]["KH_NGAYKG1"]);

                            dicKHOAHOC.Add("dd", (_NGAY_KG.Day < 10 ? "0" + _NGAY_KG.Day.ToString() : _NGAY_KG.Day.ToString()));
                            dicKHOAHOC.Add("mm", (_NGAY_KG.Month < 10 ? "0" + _NGAY_KG.Month.ToString() : _NGAY_KG.Month.ToString()));
                            dicKHOAHOC.Add("yyyy", _NGAY_KG.Year.ToString());

                            var _NGAY_BG = Convert.ToDateTime(vDataTable.Rows[0]["KH_NGAYBG1"]);
                            //   var _now = DateTime.Now;
                            dicKHOAHOC.Add("dd2", (_NGAY_BG.Day < 10 ? "0" + _NGAY_BG.Day.ToString() : _NGAY_BG.Day.ToString()));
                            dicKHOAHOC.Add("mm2", (_NGAY_BG.Month < 10 ? "0" + _NGAY_BG.Month.ToString() : _NGAY_BG.Month.ToString()));
                            dicKHOAHOC.Add("yyyy2", _NGAY_BG.Year.ToString());


                            foreach (Microsoft.Office.Interop.Word.Field field in _doc.Fields)
                            {
                                string fieldName = field.Code.Text.Substring(11, field.Code.Text.IndexOf("\\") - 12).Trim();
                                if (dicKHOAHOC.ContainsKey(fieldName))
                                {
                                    field.Select();
                                    _app.Selection.TypeText(dicKHOAHOC[fieldName]);
                                }
                            }


                        }
                        //Thông tin  học viên
                        if (ds.Tables[1].Rows.Count > 0)
                        {

                            System.Data.DataTable vDataTable = ds.Tables[1];
                            Microsoft.Office.Interop.Word.Table tbl = _doc.Tables[2];


                            int lenrow = vDataTable.Rows.Count;
                            int lencol = vDataTable.Columns.Count;

                            for (int i = 0; i < lenrow; ++i)
                            {

                                tbl.Rows.Add(ref ob);

                                for (int j = 0; j < lencol; ++j)
                                {


                                    tbl.Cell(i + 3, j + 1).Range.Text = vDataTable.Rows[i][j].ToString();// +"\v 2";
                                    //tbl.Cell(i + 2, j + 1).VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                                    tbl.Cell(i + 3, j + 1).Range.Font.Size = 10F;

                                }

                            }
                        }
                        // _doc.Tables[2].Rows[ds.Tables[1].Rows.Count + 2].Delete();

                        try
                        {

                            object filenamesave = GENERATED_FILE_NAME;
                            _doc.SaveAs2(ref filenamesave);
                            _doc.Close(ref ob, ref ob, ref ob);
                            _doc = null;
                            _app.Quit(ref ob, ref ob, ref ob);
                            _app = null;
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                        finally
                        {
                            if (_doc != null)
                            {
                                _doc.Close(ref ob, ref ob, ref ob);
                                _doc = null;
                            }
                            if (_app != null)
                            {
                                _app.Quit(ref ob, ref ob, ref ob);
                                _app = null;
                            }

                        }

                        return File(GENERATED_FILE_NAME, "application/docx", filename);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        if (_doc != null)
                        {
                            _doc.Close(ref ob, ref ob, ref ob);
                            _doc = null;
                        }
                        if (_app != null)
                        {
                            _app.Quit(ref ob, ref ob, ref ob);
                            _app = null;
                        }
                    }


                }
                ViewBag.TotalRow = model == null ? "0" : model.Count().ToString();
                var pageIndex = SearchModel.Page ?? 1;
                SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
                return View(SearchModel);
            }
            catch (Exception ex)
            {
                ViewBag.Message = string.Format("Lỗi hệ thống vui lòng liên hệ với admin.Lỗi: {0}", ex.ToString());
                return View(SearchModel);
            }
        }

        public ActionResult Search_Log(ViewModelQLDBCI_Log SearchModel, int? currentPage)
        {
            try
            {
                initialCategorySearchAction_Log(SearchModel);
                List<CrudModelDBCI_Log> model = new List<CrudModelDBCI_Log>();
                model = GetListData_Log(SearchModel);


              
                ViewBag.TotalRow = model == null ? "0" : model.Count().ToString();
                var pageIndex = SearchModel.Page ?? 1;
                SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
                return View(SearchModel);
            }
            catch (Exception ex)
            {
                ViewBag.Message = string.Format("Lỗi hệ thống vui lòng liên hệ với admin.Lỗi: {0}", ex.ToString());
                return View(SearchModel);
            }
        }

        #region"-- giangtd3 -2017-08-26"

        //ApprovedAll 
        [HttpPost]
        public ActionResult ApprovedAll(JsonDataPheDuyetBCI a)
        {
            int _count = 0;

            if (a != null)
            {
                if (a.Categories != null && a.Categories.Length > 0)
                {
                    int i = 0;
                    string[] Ids = JsonConvert.DeserializeObject<string[]>(a.Categories[0]);

                    foreach (var _key in Ids)
                    {

                        var lstMA = _key.Split('#').ToList();
                        CrudModelDBCI _model = new CrudModelDBCI();
                        _model.BCI_MA = lstMA[0];
                        _model.BCI_DVGTVT_MA = lstMA[1];
                        _model.BCI_KH_MA = lstMA[2];
                        _model.BCI_NGUOINHAP = User.Identity.Name;
                        if (lstMA[3] == "Chưa duyệt")
                        {
                            _model.BCI_TRANGTHAI = "1";
                        }
                        else if (lstMA[3] == "Đã duyệt")
                        {
                            _model.BCI_TRANGTHAI = "1";
                        }
                        else
                        {
                            _model.BCI_TRANGTHAI = "1";
                        }
                        Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_DBCI_UPDATE(_model);
                        _count++;
                    }
                }
            }

            this.Information(string.Format("Đã phê duyệt [{0}] báo cáo I thành công!", _count));
            TempData["ThongBao"] = string.Format("Đã phê duyệt [{0}] báo cáo I thành công!", _count);
            //dasfas

            string url = Url.Action("Index", "GPS_QuanLyBaoCaoI");
            return Json(new { success = true, url = url });

        }

        [HttpPost]
        public ActionResult UnApprovedAll(JsonDataPheDuyetBCI a)
        {
            int _count = 0;

            if (a != null)
            {
                if (a.Categories != null && a.Categories.Length > 0)
                {
                    int i = 0;
                    string[] Ids = JsonConvert.DeserializeObject<string[]>(a.Categories[0]);

                    foreach (var _key in Ids)
                    {

                        var lstMA = _key.Split('#').ToList();
                        CrudModelDBCI _model = new CrudModelDBCI();
                        _model.BCI_MA = lstMA[0];
                        _model.BCI_DVGTVT_MA = lstMA[1];
                        _model.BCI_KH_MA = lstMA[2];
                        _model.BCI_NGUOINHAP = User.Identity.Name;
                        if (lstMA[3] == "Chưa duyệt")
                        {
                            _model.BCI_TRANGTHAI = "0";
                        }
                        else if (lstMA[3] == "Đã duyệt")
                        {
                            _model.BCI_TRANGTHAI = "0";
                        }
                        else
                        {
                            _model.BCI_TRANGTHAI = "0";
                        }
                        Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_DBCI_UPDATE(_model);
                        _count++;
                    }
                }
            }

            this.Information(string.Format("Đã Hủy phê duyệt [{0}] báo cáo I thành công!", _count));
            TempData["ThongBao"] = string.Format("Đã Hủy phê duyệt [{0}] báo cáo I thành công!", _count);

            string url = Url.Action("Index", "GPS_QuanLyBaoCaoI");
            return Json(new { success = true, url = url });

        }

        //BCIDetail
        public ActionResult BCIDetail(string BCI_KH_MA, string BCI_DVGTVT_MA)
        {
            ViewModelQLDBCI_Details model = new ViewModelQLDBCI_Details();

            //Lấy data from database:
            DataSet ds = Singleton<GPS.GPS_DAL.GPS_HocVienDAO>.Inst.GET_BCIDetail_BY_KHOAHOC(BCI_KH_MA, BCI_DVGTVT_MA);
            if (ds.Tables[0] != null)
            {
                model.KHOAHOC = GPS.COMMON.GPS_Library.MapDataRowToObject<DKH>(ds.Tables[0].Rows[0]);
            }

            if (ds.Tables[1] != null)
            {
                model.HOCVIENs = GPS.COMMON.GPS_Library.MapDataTableToList<DHV>(ds.Tables[1]);

            }
            ViewBag.HV_COUNT = ds.Tables[1].Rows.Count;

            if (ds.Tables[2] != null)
            {
                model.PHUONGTIENs = GPS.COMMON.GPS_Library.MapDataTableToList<DPT_Metadata>(ds.Tables[2]);
            }
            ViewBag.PT_COUNT = ds.Tables[2].Rows.Count;


            if (ds.Tables[3] != null)
            {
                model.GIANGVIENs = GPS.COMMON.GPS_Library.MapDataTableToList<DGV>(ds.Tables[3]);
            }
            ViewBag.GV_COUNT = ds.Tables[3].Rows.Count;

            if (ds.Tables[4] != null)
            {
                model.LICHHOC_LTs = GPS.COMMON.GPS_Library.MapDataTableToList<ModelDKH_LH1>(ds.Tables[4]);
            }
            ViewBag.LT_COUNT = ds.Tables[4].Rows.Count;

            if (ds.Tables[5] != null)
            {
                model.LICHHOC_THs = GPS.COMMON.GPS_Library.MapDataTableToList<ModelDKH_LH1>(ds.Tables[5]);
            }
            ViewBag.TH_COUNT = ds.Tables[5].Rows.Count;

            return PartialView("_BCIDetail", model);
        }

        //PrintBCI

        public ActionResult PrintBCI(string BCI_DVGTVT_MA, string BCI_KH_MA)
        {
            ViewModelQLDBCI SearchModel = new ViewModelQLDBCI();
            SearchModel.pBCI_DVGTVT_MA = BCI_DVGTVT_MA;
            SearchModel.pBCI_KH_MA = BCI_KH_MA;

            ExportDHV_BC1 _model = BindDataExport_BaoCao1(SearchModel);
            if (_model == null)
            {
                ViewBag.Message = "Không tìm thấy dữ liệu để xuất danh sách báo cáo 1.";
                return RedirectToAction("Index");
            }
            byte[] contentFile = null;
            contentFile = GPS.ENTITYS.EXPORTWORD_DHV_BC1.EXPORT_HOCVIENBC1(Server.MapPath("~/Templates/GPS_Word/BC1.docx"), _model, "DocxGenericReport");
            string nameOutput = string.Format("DanhSachBaoCao1_{0}_{0}.docx", _model.KH_TEN, DateTime.Now.ToShortDateString().Replace("/", "_"));
            return File(contentFile, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", nameOutput);

        }


        //BCIDetail_PT
        public ActionResult BCIDetail_PT(string PT_KH_MA, string PT_DVGTVT_MA, string PT_BIENSO)
        {
            ViewModelQLDBCI_PT_Details model = new ViewModelQLDBCI_PT_Details();

            //Lấy data from database:
            DataSet ds = Singleton<GPS.GPS_DAL.GPS_HocVienDAO>.Inst.GET_BCIDetail_GET_Detail_PT(PT_KH_MA, PT_DVGTVT_MA, PT_BIENSO);
            if (ds.Tables[0] != null)
            {
                model.PHUONGTIEN = GPS.COMMON.GPS_Library.MapDataRowToObject<DPT>(ds.Tables[0].Rows[0]);
            }

            if (ds.Tables[1] != null)
            {
                model.HOCVIENs = GPS.COMMON.GPS_Library.MapDataTableToList<DHV>(ds.Tables[1]);

            }
            ViewBag.HV_COUNT = ds.Tables[1].Rows.Count;

            if (ds.Tables[2] != null)
            {
                model.GIANGVIENs = GPS.COMMON.GPS_Library.MapDataTableToList<DGV>(ds.Tables[2]);
            }
            ViewBag.GV_COUNT = ds.Tables[2].Rows.Count;
            ViewBag.PT_KH_MA = PT_KH_MA;


            return PartialView("_BCIDetail_PT", model);
        }

        //_BCIDetail_DiemDanh
        public ActionResult BCIDetail_DiemDanh(string HV_KH_MA, string HV_DVGTVT_MA,string HV_MA)
        {
            List<ViewDKH_DD> model = new List<ViewDKH_DD>();
            ViewBag.TongSo = 0;
            //Lấy data from database:
            DataSet ds = Singleton<GPS.GPS_DAL.GPS_HocVienDAO>.Inst.GET_BCIDetail_GET_Detail_DiemDanh(HV_KH_MA, HV_DVGTVT_MA, HV_MA);
            if (ds.Tables[0] != null)
            {
                model = GPS.COMMON.GPS_Library.MapDataTableToList<ViewDKH_DD>(ds.Tables[0]);
                ViewBag.TongSo = model.Count();
            }

          
            return PartialView("_BCIDetail_DiemDanh", model);
        }


        public ActionResult Export_Excel_BCI_Detail(string BCI_KH_MA, string BCI_DVGTVT_MA)
        {
            try
            {

                ViewModelQLDBCI_Details model = new ViewModelQLDBCI_Details();

                //Lấy data from database:
                DataSet ds = Singleton<GPS.GPS_DAL.GPS_HocVienDAO>.Inst.GET_BCIDetail_BY_KHOAHOC(BCI_KH_MA, BCI_DVGTVT_MA);
                if (ds.Tables[0] != null)
                {
                    model.KHOAHOC = GPS.COMMON.GPS_Library.MapDataRowToObject<DKH>(ds.Tables[0].Rows[0]);
                }

                if (ds.Tables[1] != null)
                {
                    model.HOCVIENs = GPS.COMMON.GPS_Library.MapDataTableToList<DHV>(ds.Tables[1]);

                }
                //  ViewBag.HV_COUNT = ds.Tables[1].Rows.Count;

                if (ds.Tables[2] != null)
                {
                    model.PHUONGTIENs = GPS.COMMON.GPS_Library.MapDataTableToList<DPT_Metadata>(ds.Tables[2]);
                }
                // ViewBag.PT_COUNT = ds.Tables[2].Rows.Count;


                if (ds.Tables[3] != null)
                {
                    model.GIANGVIENs = GPS.COMMON.GPS_Library.MapDataTableToList<DGV>(ds.Tables[3]);
                }
                //ViewBag.GV_COUNT = ds.Tables[3].Rows.Count;

                if (ds.Tables[4] != null)
                {
                    model.LICHHOC_LTs = GPS.COMMON.GPS_Library.MapDataTableToList<ModelDKH_LH1>(ds.Tables[4]);
                }
                //    ViewBag.LT_COUNT = ds.Tables[4].Rows.Count;

                if (ds.Tables[5] != null)
                {
                    model.LICHHOC_THs = GPS.COMMON.GPS_Library.MapDataTableToList<ModelDKH_LH1>(ds.Tables[5]);
                }


                FileInfo template = new FileInfo(Server.MapPath(@"\Templates\GPS_Excel\BCI_Detail.xlsx"));
                FileInfo newFile = new FileInfo(Server.MapPath(@"\Templates\GPS_Excel\BCI_Detail_Export.xlsx"));
                // Using the template to create the newFile...

                using (OfficeOpenXml.ExcelPackage excelPackage = new OfficeOpenXml.ExcelPackage(newFile, template))
                {
                    ExcelWorkbook myWorkbook = excelPackage.Workbook;

                    if (model.KHOAHOC != null)
                    {
                        ExcelWorksheet myWorksheet_KH = myWorkbook.Worksheets["KhoaHoc"];
                        GPS.DataAccessLayer.DbContextHelper<GPS.DataAccessLayer.GPS_NVContext> _db = GPS.COMMON.Singleton<GPS.DataAccessLayer.DbContextHelper<GPS.DataAccessLayer.GPS_NVContext>>.Inst;
                        var gtvt = _db.GetOne<GPS.DataAccessLayer.SDONVIGTVT>(o => o.DVGTVT_MA == model.KHOAHOC.KH_DVGTVT_MA);
                        string dvgtvt = string.Empty;
                        if (gtvt != null)
                        {
                            dvgtvt = gtvt.DVGTVT_MA + " - " + gtvt.DVGTVT_TEN;
                        }
                        myWorksheet_KH.Cells[1, 4].Value = dvgtvt;
                        myWorksheet_KH.Cells[2, 4].Value = model.KHOAHOC.KH_MA + "- " + model.KHOAHOC.KH_TEN;
                        myWorksheet_KH.Cells[3, 4].Value = model.KHOAHOC.KH_HANGDT_MA;
                        myWorksheet_KH.Cells[4, 4].Value = model.KHOAHOC.KH_HANGGPLX_MA;
                        if (model.KHOAHOC.KH_NGAYKG != null)
                            myWorksheet_KH.Cells[5, 4].Value = model.KHOAHOC.KH_NGAYKG.Value.ToString("dd/MM/yyyy");
                        if (model.KHOAHOC.KH_NGAYBG != null)
                            myWorksheet_KH.Cells[6, 4].Value = model.KHOAHOC.KH_NGAYBG.Value.ToString("dd/MM/yyyy");

                    }

                    if (model.HOCVIENs != null && model.HOCVIENs.Count > 0)
                    {
                        ExcelWorksheet myWorksheet_HV = myWorkbook.Worksheets["HocVien"];
                        for (int i = 0; i < model.HOCVIENs.Count; i++)
                        {
                            myWorksheet_HV.Cells[i + 2, 1].Value = model.HOCVIENs[i].STT;
                            myWorksheet_HV.Cells[i + 2, 2].Value = model.HOCVIENs[i].HV_MA;
                            myWorksheet_HV.Cells[i + 2, 3].Value = model.HOCVIENs[i].HV_HOTEN;
                            myWorksheet_HV.Cells[i + 2, 4].Value = model.HOCVIENs[i].HV_SOCMT;
                            myWorksheet_HV.Cells[i + 2, 5].Value = model.HOCVIENs[i].HV_NGAYSINH;
                            myWorksheet_HV.Cells[i + 2, 6].Value = model.HOCVIENs[i].HV_GIOITINH == "M" ? "Nam" : "Nữ";
                            myWorksheet_HV.Cells[i + 2, 7].Value = model.HOCVIENs[i].HV_NOITT;
                            myWorksheet_HV.Cells[i + 2, 8].Value = model.HOCVIENs[i].HV_HANGDT_MA;
                        }
                    }

                    if (model.PHUONGTIENs != null && model.PHUONGTIENs.Count > 0)
                    {
                        ExcelWorksheet myWorksheet_PT = myWorkbook.Worksheets["PhuongTien"];
                        for (int i = 0; i < model.PHUONGTIENs.Count; i++)
                        {
                            myWorksheet_PT.Cells[i + 2, 1].Value = (i + 1);
                            myWorksheet_PT.Cells[i + 2, 2].Value = model.PHUONGTIENs[i].PT_BIENSO;
                            myWorksheet_PT.Cells[i + 2, 3].Value = model.PHUONGTIENs[i].PT_HANGXE;
                            myWorksheet_PT.Cells[i + 2, 4].Value = model.PHUONGTIENs[i].PT_NHANHIEU;
                            myWorksheet_PT.Cells[i + 2, 5].Value = model.PHUONGTIENs[i].PT_LOAIXE;
                            myWorksheet_PT.Cells[i + 2, 6].Value = model.PHUONGTIENs[i].PT_SOMAY;
                            myWorksheet_PT.Cells[i + 2, 7].Value = model.PHUONGTIENs[i].PT_NAMSX;
                            if (model.PHUONGTIENs[i].PT_NGAYHHGPXTL != null)
                                myWorksheet_PT.Cells[i + 2, 8].Value = model.PHUONGTIENs[i].PT_NGAYHHGPXTL.Value.ToString("dd/MM/yyyy");
                        }

                    }

                    if (model.GIANGVIENs != null && model.GIANGVIENs.Count > 0)
                    {
                        ExcelWorksheet myWorksheet_GV = myWorkbook.Worksheets["GiangVien"];
                        for (int i = 0; i < model.GIANGVIENs.Count; i++)
                        {
                            myWorksheet_GV.Cells[i + 2, 1].Value = (i + 1);
                            myWorksheet_GV.Cells[i + 2, 2].Value = model.GIANGVIENs[i].GV_MA;
                            myWorksheet_GV.Cells[i + 2, 3].Value = model.GIANGVIENs[i].GV_HODEM + model.GIANGVIENs[i].GV_TEN;
                            myWorksheet_GV.Cells[i + 2, 4].Value = model.GIANGVIENs[i].GV_NGAYSINH;
                            myWorksheet_GV.Cells[i + 2, 5].Value = model.GIANGVIENs[i].GV_SOCMT;
                            myWorksheet_GV.Cells[i + 2, 6].Value = model.GIANGVIENs[i].GV_TEL;
                            myWorksheet_GV.Cells[i + 2, 7].Value = model.GIANGVIENs[i].GV_HANGDT_MA;
                            myWorksheet_GV.Cells[i + 2, 8].Value = model.GIANGVIENs[i].GV_GIOITINH == "M" ? "Nam" : "Nữ";
                            myWorksheet_GV.Cells[i + 2, 9].Value = model.GIANGVIENs[i].GV_HGPLX_MA;
                            myWorksheet_GV.Cells[i + 2, 10].Value = model.GIANGVIENs[i].GV_NGAYCAPGPLX;
                            myWorksheet_GV.Cells[i + 2, 11].Value = model.GIANGVIENs[i].GV_TDCHUYENMON;

                        }

                    }

                    if (model.LICHHOC_LTs != null && model.LICHHOC_LTs.Count > 0)
                    {
                        ExcelWorksheet myWorksheet_LT = myWorkbook.Worksheets["LyThuyet"];
                        for (int i = 0; i < model.LICHHOC_LTs.Count; i++)
                        {
                            myWorksheet_LT.Cells[i + 2, 1].Value = (i + 1);
                            myWorksheet_LT.Cells[i + 2, 2].Value = model.LICHHOC_LTs[i].KH_LH_THANG;
                            myWorksheet_LT.Cells[i + 2, 3].Value = model.LICHHOC_LTs[i].KH_LH_TUAN;
                            if (model.LICHHOC_LTs[i].KH_LH_TUNGAY != null)
                                myWorksheet_LT.Cells[i + 2, 4].Value = model.LICHHOC_LTs[i].KH_LH_TUNGAY.Value.ToString("dd/MM/yyyy");
                            if (model.LICHHOC_LTs[i].KH_LH_DENNGAY != null)
                                myWorksheet_LT.Cells[i + 2, 5].Value = model.LICHHOC_LTs[i].KH_LH_DENNGAY.Value.ToString("dd/MM/yyyy");

                            myWorksheet_LT.Cells[i + 2, 6].Value = model.LICHHOC_LTs[i].GV_HOTEN;
                            myWorksheet_LT.Cells[i + 2, 7].Value = model.LICHHOC_LTs[i].KH_LH_MONHOC;

                        }
                    }

                    if (model.LICHHOC_THs != null && model.LICHHOC_THs.Count > 0)
                    {
                        ExcelWorksheet myWorksheet_TH = myWorkbook.Worksheets["ThucHanh"];
                        for (int i = 0; i < model.LICHHOC_THs.Count; i++)
                        {
                            myWorksheet_TH.Cells[i + 2, 1].Value = (i + 1);
                            myWorksheet_TH.Cells[i + 2, 2].Value = model.LICHHOC_THs[i].KH_LH_PT_BIENSO;
                            myWorksheet_TH.Cells[i + 2, 3].Value = model.LICHHOC_THs[i].KH_LH_THANG;
                            myWorksheet_TH.Cells[i + 2, 4].Value = model.LICHHOC_THs[i].KH_LH_TUAN;
                            if (model.LICHHOC_THs[i].KH_LH_TUNGAY != null)
                                myWorksheet_TH.Cells[i + 2, 5].Value = model.LICHHOC_THs[i].KH_LH_TUNGAY.Value.ToString("dd/MM/yyyy");
                            if (model.LICHHOC_THs[i].KH_LH_DENNGAY != null)
                                myWorksheet_TH.Cells[i + 2, 6].Value = model.LICHHOC_THs[i].KH_LH_DENNGAY.Value.ToString("dd/MM/yyyy");

                            myWorksheet_TH.Cells[i + 2, 7].Value = model.LICHHOC_THs[i].GV_HOTEN;
                          
                        }
                    }

                    excelPackage.Save();
                }
                string contentType = string.Empty;
                string CurrentFileName = Server.MapPath(@"\Templates\GPS_Excel\BCI_Detail_Export.xlsx");
                if (CurrentFileName.Contains(".xlsx"))
                {
                    contentType = "application/msexcel";
                }
                return File(CurrentFileName, contentType, string.Format("BCI_Detail_{0}.xlsx", DateTime.Now.ToShortDateString().Replace("/", "_")));



            }
            catch (Exception ex)
            {
                throw ex;
            }



        }

        public ActionResult Export_Excel_BCI_Detail_PT(string PT_KH_MA, string PT_DVGTVT_MA, string PT_BIENSO)
        {
            try
            {


                ViewModelQLDBCI_PT_Details model = new ViewModelQLDBCI_PT_Details();

                //Lấy data from database:
                DataSet ds = Singleton<GPS.GPS_DAL.GPS_HocVienDAO>.Inst.GET_BCIDetail_GET_Detail_PT(PT_KH_MA, PT_DVGTVT_MA, PT_BIENSO);
                if (ds.Tables[0] != null)
                {
                    model.PHUONGTIEN = GPS.COMMON.GPS_Library.MapDataRowToObject<DPT>(ds.Tables[0].Rows[0]);
                }

                if (ds.Tables[1] != null)
                {
                    model.HOCVIENs = GPS.COMMON.GPS_Library.MapDataTableToList<DHV>(ds.Tables[1]);

                }
                ViewBag.HV_COUNT = ds.Tables[1].Rows.Count;

                if (ds.Tables[2] != null)
                {
                    model.GIANGVIENs = GPS.COMMON.GPS_Library.MapDataTableToList<DGV>(ds.Tables[2]);
                }
                ViewBag.GV_COUNT = ds.Tables[2].Rows.Count;
                ViewBag.PT_KH_MA = PT_KH_MA;

                FileInfo template = new FileInfo(Server.MapPath(@"\Templates\GPS_Excel\BCI_Detail_PT.xlsx"));
                FileInfo newFile = new FileInfo(Server.MapPath(@"\Templates\GPS_Excel\BCI_Detail_PT_Export.xlsx"));
                // Using the template to create the newFile...

                using (OfficeOpenXml.ExcelPackage excelPackage = new OfficeOpenXml.ExcelPackage(newFile, template))
                {
                    ExcelWorkbook myWorkbook = excelPackage.Workbook;

                    if (model.PHUONGTIEN != null)
                    {
                        ExcelWorksheet myWorksheet_PT = myWorkbook.Worksheets["PhuongTien"];
                        GPS.DataAccessLayer.DbContextHelper<GPS.DataAccessLayer.GPS_NVContext> _db = GPS.COMMON.Singleton<GPS.DataAccessLayer.DbContextHelper<GPS.DataAccessLayer.GPS_NVContext>>.Inst;
                        var gtvt = _db.GetOne<GPS.DataAccessLayer.SDONVIGTVT>(o => o.DVGTVT_MA == model.PHUONGTIEN.PT_DVGTVT_MA);
                        string dvgtvt = string.Empty;
                        if (gtvt != null)
                        {
                            dvgtvt = gtvt.DVGTVT_MA + " - " + gtvt.DVGTVT_TEN;
                        }
                        myWorksheet_PT.Cells[1, 4].Value = dvgtvt;
                        myWorksheet_PT.Cells[2, 4].Value = model.PHUONGTIEN.PT_BIENSO;

                        if (model.PHUONGTIEN.PT_NGAYGPXTL != null)
                            myWorksheet_PT.Cells[3, 4].Value = model.PHUONGTIEN.PT_NGAYGPXTL.Value.ToString("dd/MM/yyyy");
                        if (model.PHUONGTIEN.PT_NGAYHHGPXTL != null)
                            myWorksheet_PT.Cells[4, 4].Value = model.PHUONGTIEN.PT_NGAYHHGPXTL.Value.ToString("dd/MM/yyyy");

                    }

                    if (model.HOCVIENs != null && model.HOCVIENs.Count > 0)
                    {
                        ExcelWorksheet myWorksheet_HV = myWorkbook.Worksheets["HocVien"];
                        for (int i = 0; i < model.HOCVIENs.Count; i++)
                        {
                            myWorksheet_HV.Cells[i + 2, 1].Value = model.HOCVIENs[i].STT;
                            myWorksheet_HV.Cells[i + 2, 2].Value = model.HOCVIENs[i].HV_MA;
                            myWorksheet_HV.Cells[i + 2, 3].Value = model.HOCVIENs[i].HV_HOTEN;
                            myWorksheet_HV.Cells[i + 2, 4].Value = model.HOCVIENs[i].HV_SOCMT;
                            myWorksheet_HV.Cells[i + 2, 5].Value = model.HOCVIENs[i].HV_NGAYSINH;
                            myWorksheet_HV.Cells[i + 2, 6].Value = model.HOCVIENs[i].HV_GIOITINH == "M" ? "Nam" : "Nữ";
                            myWorksheet_HV.Cells[i + 2, 7].Value = model.HOCVIENs[i].HV_NOITT;
                            myWorksheet_HV.Cells[i + 2, 8].Value = model.HOCVIENs[i].HV_HANGDT_MA;
                        }
                    }

                    if (model.GIANGVIENs != null && model.GIANGVIENs.Count > 0)
                    {
                        ExcelWorksheet myWorksheet_GV = myWorkbook.Worksheets["GiangVien"];
                        for (int i = 0; i < model.GIANGVIENs.Count; i++)
                        {
                            myWorksheet_GV.Cells[i + 2, 1].Value = (i + 1);
                            myWorksheet_GV.Cells[i + 2, 2].Value = model.GIANGVIENs[i].GV_MA;
                            myWorksheet_GV.Cells[i + 2, 3].Value = model.GIANGVIENs[i].GV_HODEM + model.GIANGVIENs[i].GV_TEN;
                            myWorksheet_GV.Cells[i + 2, 4].Value = model.GIANGVIENs[i].GV_NGAYSINH;
                            myWorksheet_GV.Cells[i + 2, 5].Value = model.GIANGVIENs[i].GV_SOCMT;
                            myWorksheet_GV.Cells[i + 2, 6].Value = model.GIANGVIENs[i].GV_TEL;
                            myWorksheet_GV.Cells[i + 2, 7].Value = model.GIANGVIENs[i].GV_HANGDT_MA;
                            myWorksheet_GV.Cells[i + 2, 8].Value = model.GIANGVIENs[i].GV_GIOITINH == "M" ? "Nam" : "Nữ";
                            myWorksheet_GV.Cells[i + 2, 9].Value = model.GIANGVIENs[i].GV_HGPLX_MA;
                            myWorksheet_GV.Cells[i + 2, 10].Value = model.GIANGVIENs[i].GV_NGAYCAPGPLX;
                            myWorksheet_GV.Cells[i + 2, 11].Value = model.GIANGVIENs[i].GV_TDCHUYENMON;
                        }
                    }

                  

                    excelPackage.Save();
                }
                string contentType = string.Empty;
                string CurrentFileName = Server.MapPath(@"\Templates\GPS_Excel\BCI_Detail_PT_Export.xlsx");
                if (CurrentFileName.Contains(".xlsx"))
                {
                    contentType = "application/msexcel";
                }
                return File(CurrentFileName, contentType, string.Format("BCI_Detail_PT_{0}.xlsx", DateTime.Now.ToShortDateString().Replace("/", "_")));


            }
            catch(Exception ex)
            {
                throw ex;
            }
           
        }

        #endregion

        public ActionResult Detail(string id, string id1, ViewModelDetailKH_HV SearchModel, int? currentPage)
        {
            string DVGTVT_MA = id;
            string KH_MA = id1;
            List<CrudModelITHHOCVIEN> model = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GETDS_SEARCH_HOCVIEN1(DVGTVT_MA, "", KH_MA
                                                                                       , ""
                                                                                       , ""
                                                                                       , ""
                                                                                       , ""
                                                                                       , "1"
                                                                                      );

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return PartialView(SearchModel);
        }
        #endregion

        #region Getdatabinding

        public List<CrudModelDBCI_Log> GetListData_Log(ViewModelQLDBCI_Log SearchModel)
        {
            var pBCI_KH_MA = new OracleParameter("pBCI_KH_MA", OracleDbType.Varchar2, (SearchModel.pBCI_KH_MA == null ? DBNull.Value : (object)SearchModel.pBCI_KH_MA), ParameterDirection.Input);
            var pBCI_DVGTVT_MA = new OracleParameter("pBCI_DVGTVT_MA", OracleDbType.Varchar2, (SearchModel.pBCI_DVGTVT_MA == null ? DBNull.Value : (object)SearchModel.pBCI_DVGTVT_MA.Trim()), ParameterDirection.Input);
            var pBCI_MA = new OracleParameter("pBCI_MA", OracleDbType.Varchar2, (SearchModel.pBCI_MA == null ? DBNull.Value : (object)SearchModel.pBCI_MA.Trim()), ParameterDirection.Input);
            var pBCI_TRANGTHAI = new OracleParameter("pBCI_TRANGTHAI", OracleDbType.Decimal, (SearchModel.pBCI_TRANGTHAI == null ? DBNull.Value : (object)SearchModel.pBCI_TRANGTHAI.Trim()), ParameterDirection.Input);
            var pTUNGAY = new OracleParameter("pTUNGAY", OracleDbType.Date, (SearchModel.pBCI_TUNGAY == null ? DBNull.Value : (object)SearchModel.pBCI_TUNGAY), ParameterDirection.Input);
            var pDENNGAY = new OracleParameter("pDENNGAY", OracleDbType.Date, (SearchModel.pBCI_DENNGAY == null ? DBNull.Value : (object)SearchModel.pBCI_DENNGAY), ParameterDirection.Input);
            var _pRET = new OracleParameter("pRET", OracleDbType.RefCursor, ParameterDirection.Output);
            var entities = _db.DbContext.Database.SqlQuery<CrudModelDBCI_Log>("BEGIN GPS_DAOTAO.GPS_DAOTAO_GET_DBCI_LOG(:pBCI_KH_MA,:pBCI_DVGTVT_MA,:pBCI_MA,:pBCI_TRANGTHAI,:pTUNGAY, :pDENNGAY,:pRET); end;"
                                        , pBCI_KH_MA
                                        , pBCI_DVGTVT_MA
                                        , pBCI_MA
                                        , pBCI_TRANGTHAI
                                        , pTUNGAY
                                        , pDENNGAY
                                        , _pRET).ToList();
            return entities;
        }
        public List<CrudModelDBCI> GetListData(ViewModelQLDBCI SearchModel)
        {
            var pBCI_KH_MA = new OracleParameter("pBCI_KH_MA", OracleDbType.Varchar2, (SearchModel.pBCI_KH_MA == null ? DBNull.Value : (object)SearchModel.pBCI_KH_MA), ParameterDirection.Input);
            var pBCI_DVGTVT_MA = new OracleParameter("pBCI_DVGTVT_MA", OracleDbType.Varchar2, (SearchModel.pBCI_DVGTVT_MA == null ? DBNull.Value : (object)SearchModel.pBCI_DVGTVT_MA.Trim()), ParameterDirection.Input);
            var pBCI_MA = new OracleParameter("pBCI_MA", OracleDbType.Varchar2, (SearchModel.pBCI_MA == null ? DBNull.Value : (object)SearchModel.pBCI_MA.Trim()), ParameterDirection.Input);
            var pBCI_TRANGTHAI = new OracleParameter("pBCI_TRANGTHAI", OracleDbType.Decimal, (SearchModel.pBCI_TRANGTHAI == null ? DBNull.Value : (object)SearchModel.pBCI_TRANGTHAI.Trim()), ParameterDirection.Input);
            var pTUNGAY = new OracleParameter("pTUNGAY", OracleDbType.Date, (SearchModel.pBCI_TUNGAY == null ? DBNull.Value : (object)SearchModel.pBCI_TUNGAY), ParameterDirection.Input);
            var pDENNGAY = new OracleParameter("pDENNGAY", OracleDbType.Date, (SearchModel.pBCI_DENNGAY == null ? DBNull.Value : (object)SearchModel.pBCI_DENNGAY), ParameterDirection.Input);
            var pBCI_HANGDT_MA = new OracleParameter("pBCI_HANGDT_MA", OracleDbType.Varchar2, (SearchModel.pBCI_HANGDT_MA == null ? DBNull.Value : (object)SearchModel.pBCI_HANGDT_MA), ParameterDirection.Input);
            var _pRET = new OracleParameter("pRET", OracleDbType.RefCursor, ParameterDirection.Output);
            var entities = _db.DbContext.Database.SqlQuery<CrudModelDBCI>("BEGIN GPS_DAOTAO.GPS_DAOTAO_GET_DBCI(:pBCI_KH_MA,:pBCI_DVGTVT_MA,:pBCI_MA,:pBCI_TRANGTHAI,:pTUNGAY, :pDENNGAY,:pBCI_HANGDT_MA,:pRET); end;"
                                        , pBCI_KH_MA
                                        , pBCI_DVGTVT_MA
                                        , pBCI_MA
                                        , pBCI_TRANGTHAI
                                        , pTUNGAY
                                        , pDENNGAY
                                        , pBCI_HANGDT_MA
                                        , _pRET).ToList();
            return entities;
        }

        public ExportDHV_BC1 BindDataExport_BaoCao1(ViewModelQLDBCI model)
        {
            ExportDHV_BC1 _model = new ExportDHV_BC1();
            DataSet ds = OracleHelper.ExecuteDataset(_db.DbContext.Database.Connection.ConnectionString, CommandType.StoredProcedure, "GPS_DAOTAO.GPS_DAOTAO_DSHVLX"
                                                    , new OracleParameter("PDVGTVT_MA", OracleDbType.Varchar2, string.IsNullOrWhiteSpace(model.pBCI_DVGTVT_MA) ? DBNull.Value : (object)model.pBCI_DVGTVT_MA, ParameterDirection.Input)
                                                    , new OracleParameter("PKH_MA", OracleDbType.Varchar2, string.IsNullOrWhiteSpace(model.pBCI_KH_MA) ? DBNull.Value : (object)model.pBCI_KH_MA, ParameterDirection.Input)
                                                    , new OracleParameter("PRETHEADER", OracleDbType.RefCursor, ParameterDirection.Output)
                                                    , new OracleParameter("PRETDSHVLX", OracleDbType.RefCursor, ParameterDirection.Output));
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                _model.DVGTVT_TEN = ds.Tables[0].Rows[i]["DVGTVT_TEN"] == null ? "" : ds.Tables[0].Rows[i]["DVGTVT_TEN"].ToString();
                _model.KH_HANGGPLX_MA = ds.Tables[0].Rows[i]["KH_HANGGPLX_MA"] == null ? "" : ds.Tables[0].Rows[i]["KH_HANGGPLX_MA"].ToString();
                _model.KH_TEN = ds.Tables[0].Rows[i]["KH_TEN"] == null ? "" : ds.Tables[0].Rows[i]["KH_TEN"].ToString();
                _model.KH_THOIGIANDT = ds.Tables[0].Rows[i]["KH_NGAYBG"] == null ? "" : ds.Tables[0].Rows[i]["KH_NGAYBG"].ToString();
                _model.KH_NGAYKG = ds.Tables[0].Rows[i]["KH_NGAYKG"] == null ? "" : ds.Tables[0].Rows[i]["KH_NGAYKG"].ToString();
                _model.KH_THOIGIANDT = ds.Tables[0].Rows[i]["KH_THOIGIANDT"] == null ? "" : ds.Tables[0].Rows[i]["KH_THOIGIANDT"].ToString();
            }
            _model.LIST_HS = COMMON.Helpers.MapDataTableToList<ExportDHV_BC1>(ds.Tables[1]);
            int icount = 1;
            foreach (var item in _model.LIST_HS)
            {
                item.STT = icount.ToString();
                icount++;
            }
            _model.DATETIME_NOW = "Ngày " + DateTime.Now.Day.ToString() + " tháng " + DateTime.Now.Month.ToString() + " năm" + DateTime.Now.Year.ToString();
            return _model;
        }
        #endregion

        #region phê duyệt báo cáo I
        public ActionResult Approval(string id, string id1, string id2, decimal id3)
        {
            CrudModelDBCI model = new CrudModelDBCI();
            model.BCI_MA = id;
            model.BCI_DVGTVT_MA = id1;
            model.BCI_KH_MA = id2;
            model.BCI_TRANGTHAI = id3.ToString();
            return PartialView("Approval", model);
        }

        [HttpPost]
        public ActionResult Approval(CrudModelDBCI model, FormCollection form)
        {
            try
            {
                model.BCI_NGUOINHAP = User.Identity.Name;
                Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_DBCI_UPDATE(model);
                string url = Url.Action("Index", "GPS_QuanLyBaoCaoI");
                return Json(new { success = true, url = url });
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("ErrorMsg", ex.Message);
                return RedirectToAction("Index");
            }
        }
        #endregion

        #region"Ajax"

        [HttpPost]
        public ActionResult getKH_MAs(string ma_DVGT)
        {
            string _ma = ma_DVGT.Split(':')[0];
            TempData["DVGTVT_MA"] = ma_DVGT;
            var districts = _db.Filter<DKH>(d => d.KH_DVGTVT_MA == _ma).Select(a => "<option value='" + a.KH_MA + "'>" + a.KH_MA + "</option>");
            return Content(string.Join("", districts));
        }

        [HttpPost]
        public ActionResult getHV_MA(string gv_DVGTVT_MA)
        {
            string strHV_MA = gv_DVGTVT_MA + "-" + DateTime.Now.ToString("yyyyMMdd HH:mm:ss").Replace(" ", "-").Replace(":", "");

            return Content(string.Join("", strHV_MA));
        }

        public JsonResult AutoCompleteNOICT_DVHC_MA(string term)
        {
            var result = _db.Filter<SDONVIHC>(o => o.DVHC_MA.Contains(term) || o.DVHC_TENDAYDU.ToUpper().Contains(term.ToUpper()));
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult AutoCompleteDVGTVT_MA(string term)
        {
            TempData["DVGTVT_MA"] = term;
            var result = _db.Filter<SDONVIGTVT>(o => (o.DVGTVT_MAQL == "40")
                                                    && (o.DVGTVT_MA.Contains(term) || o.DVGTVT_TEN.ToUpper().Contains(term.ToUpper()))
                                                    && (o.DVGTVT_ACTIVE == "1"));
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult AutoCompleteKH_MA(string term, string dvgtvt_ma)
        {
            string strDVGTVT_MA = dvgtvt_ma.Split(':')[0];
            var result = _db.Filter<DKH>(o => (o.KH_TEN.ToUpper().Contains(term.ToUpper()) || o.KH_MA.ToUpper().Contains(term.ToUpper()))
                                                && (o.KH_DVGTVT_MA == null || o.KH_DVGTVT_MA.Contains(strDVGTVT_MA))
                                                && (o.KH_HANGDT_RANK >= 5)
                                                && (o.KH_ACTIVE == "1"));
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region disable
        //#region Create
        //public ActionResult Create()
        //{
        //    initialCategoryCreateAction();

        //    return View(new CrudModelDHV());
        //}

        //[HttpPost]
        //public ActionResult Create(CrudModelDHV model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        DHV _model = new DHV();
        //        GPS.COMMON.Helpers.CopyObject<DHV>(model, ref _model, new List<string> { "HV_ACTIVE" });
        //        _model.HV_ACTIVE = (model.HV_ACTIVE == false ? "0" : "1");
        //        _model.HV_DVGTVT_MA = model.HV_DVGTVT_MA.Split(':')[0];
        //        _model.ID = EFSequence.GetNextValue("SEQ_DHV_ID");
        //        if (!string.IsNullOrWhiteSpace(model.HV_NOITT_DVHC_MA))
        //        {
        //            _model.HV_NOITT_DVHC_MA = model.HV_NOITT_DVHC_MA.Split(':')[0];
        //        }

        //        if (!string.IsNullOrWhiteSpace(model.HV_NOITT_DVHC_DVQL))
        //        {
        //            _model.HV_NOITT_DVHC_DVQL = model.HV_NOITT_DVHC_DVQL.Split(':')[0];
        //        }
        //        _model.HV_MA = GetIdentity_HVMa(_model.HV_DVGTVT_MA);
        //        _model.HV_NGUOINHAP = User.Identity.Name;
        //        _model.HV_NGAYNHAP = DateTime.Now;
        //        _db.Insert<DHV>(_model);
        //        return RedirectToAction("Index");
        //    }
        //    return View(model);
        //}
        //public void initialCategoryCreateAction()
        //{
        //    ViewBag.HV_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
        //    ViewBag.HV_HANGDT_MAs = new SelectList(_db.GetAll<SHANGDT>(), "HANGDT_MA", "HANGDT_TEN");
        //    ViewBag.HV_NOICAPCMTs = new SelectList(_db.Filter<SDONVIHC>(o => o.DVHC_LOAI == "TI"), "DVHC_MA", "DVHC_TENDAYDU");
        //    ViewBag.HV_QUOCTICH_MAs = new SelectList(_db.GetAll<SQUOCTICH>(), "QTICH_MA", "QTICH_TENEN");
        //    ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
        //}
        //#endregion

        //#region Edit
        //public ActionResult Edit(decimal id)
        //{

        //    DHV model = _db.GetOne<DHV>(o => o.ID == id);
        //    CrudModelDHV _model = new CrudModelDHV();
        //    GPS.COMMON.Helpers.CopyObject<CrudModelDHV>(model, ref _model, new List<string> { "HV_ACTIVE" });
        //    _model.HV_ACTIVE = (model.HV_ACTIVE == "0" ? false : true);

        //    var dvgtvt = _db.GetOne<SDONVIGTVT>(o => o.DVGTVT_MA == model.HV_DVGTVT_MA);
        //    _model.HV_DVGTVT_MA = dvgtvt.DVGTVT_MA + ":" + dvgtvt.DVGTVT_TEN;

        //    if (!string.IsNullOrWhiteSpace(model.HV_NOITT_DVHC_MA))
        //    {
        //        var dvghc = _db.GetOne<SDONVIHC>(o => o.DVHC_MA == model.HV_NOITT_DVHC_MA);
        //        _model.HV_NOITT_DVHC_MA = dvghc.DVHC_MA + ":" + dvghc.DVHC_TENDAYDU;
        //    }

        //    if (!string.IsNullOrWhiteSpace(model.HV_NOITT_DVHC_DVQL))
        //    {
        //        var dvghc = _db.GetOne<SDONVIHC>(o => o.DVHC_MA == model.HV_NOITT_DVHC_DVQL);
        //        _model.HV_NOITT_DVHC_DVQL = dvghc.DVHC_MA + ":" + dvghc.DVHC_TENDAYDU;
        //    }

        //    initialCategoryEditAction(_model);
        //    return View(_model);
        //}

        //[HttpPost]
        //public ActionResult Edit(CrudModelDHV model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        var _model = _db.GetOne<DHV>(o => o.ID == model.ID);
        //        GPS.COMMON.Helpers.CopyObject<DHV>(model, ref _model, new List<string> { "HV_ACTIVE", "HV_NGUOINHAP", "HV_NGAYNHAP" });
        //        _model.HV_DVGTVT_MA = model.HV_DVGTVT_MA.Split(':')[0];

        //        if (!string.IsNullOrWhiteSpace(model.HV_NOITT_DVHC_MA))
        //        {
        //            _model.HV_NOITT_DVHC_MA = model.HV_NOITT_DVHC_MA.Split(':')[0];
        //        }

        //        if (!string.IsNullOrWhiteSpace(model.HV_NOITT_DVHC_DVQL))
        //        {
        //            _model.HV_NOITT_DVHC_DVQL = model.HV_NOITT_DVHC_DVQL.Split(':')[0];
        //        }
        //        _model.HV_NGUOISUA = User.Identity.Name;
        //        _model.HV_NGAYSUA = DateTime.Now;


        //        _db.Update<DHV>(_model);
        //        return RedirectToAction("Index");
        //    }

        //    return View(model);
        //}


        //public string GetIdentity_HVMa(string gv_DVGTVT_MA)
        //{
        //    string rt = "";
        //    string strHV_MA = gv_DVGTVT_MA + "-" + DateTime.Now.ToString("yyyyMMdd HH:mm:ss").Replace(" ", "-").Replace(":", "");
        //    rt = string.Join("", strHV_MA);
        //    return rt;
        //}
        //public void initialCategoryEditAction(CrudModelDHV view)
        //{
        //    ViewBag.HV_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
        //    ViewBag.HV_HANGDT_MAs = new SelectList(_db.GetAll<SHANGDT>(), "HANGDT_MA", "HANGDT_TEN");
        //    ViewBag.HV_NOICAPCMTs = new SelectList(_db.Filter<SDONVIHC>(o => o.DVHC_LOAI == "TI"), "DVHC_MA", "DVHC_TENDAYDU");
        //    ViewBag.HV_QUOCTICH_MAs = new SelectList(_db.GetAll<SQUOCTICH>(), "QTICH_MA", "QTICH_TENEN");


        //    ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
        //    string _ma = view.HV_DVGTVT_MA.Split(':')[0];
        //    var khoahocs = _db.Filter<DKH>(o => o.KH_DVGTVT_MA == _ma);


        //    ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");


        //}
        //#endregion

        //#region Delete
        //public ActionResult Delete(decimal id)
        //{
        //    CrudModelDHV _model = new CrudModelDHV();
        //    DHV model = _db.GetOne<DHV>(o => o.ID == id);
        //    _db.DeleteItem<DHV>(model);
        //    return RedirectToAction("Index");
        //}
        //#endregion

        #endregion
    }

    public class JsonDataPheDuyetBCI
    {
        public decimal DTTN_DTTN_QD_ID { get; set; }
        public string[] Categories { get; set; }
    }
}